﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Linq;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TEXORDLIST
    {
        public TEXORDLIST()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_EXORD();
            this.DataContext = MV;
            ModeEdit = true;
            InitializeComponent();
            KRS = app.TERMNO.PadRight(2).Substring(0, 2);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster != null)
            {

                filter = "VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
                sort = "VKPAUFPOS DESC";
                rows = TBMain.Select(filter, sort);
                foreach (DataRow row in rows)
                {
                    MAXORD = int.Parse(dwMaster["VKPAUFLFD"].ToString());
                    pos = int.Parse(row["VKPAUFPOS"].ToString());
                    break;
                }
            }
        }
        DataTable TBResult;

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("Please choose a record");
                return;
            }
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["VKPART"] = "PleaseEnter";
            pos++;
            dwData["VKPAUFPOS"] = Helpers.Helper.Right("000" + pos.ToString(), 3);
            dwData["ROWNID"] = dwData["VKPAUFKRS"].ToString() + "." + dwData["VKPAUFLFD"].ToString() + "." + Helpers.Helper.Right("000" + dwData["VKPAUFPOS"].ToString(), 3);

            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_EXORD(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }

        private void Load_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                DataSet Set = new DataSet();
                TMV.sql = "SELECT * FROM ICC_EXORD where VKPART not like '%PleaseEnter%'";
                TBResult = TMV.SQLiteDatabase.GetDataTable(TMV.sql);
                TBMain.Clear();
                TBResult.TableName = TMV.TableName;
                Set.Merge(TBResult);
                try
                {
                    MergeData(Set);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
                cmbNDate.Items.Clear();
                var query = from date in TBMain.AsEnumerable()
                            group date by date.Field<string>("VKPNEUDAT") into g
                            select new
                            {
                                KEY = g.Key
                            };
                foreach (var date in query)
                {
                    ComboBoxItem cb = new ComboBoxItem();
                    cmbNDate.Items.Add(cb);
                    cb.Content = date.KEY.ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //filter = "VKPAUFLFD LIKE '" + txtLFD.Text.ToString().Trim() + "%' AND VKPART LIKE '" + ART.Trim() + "%' AND VKPNEUDAT LIKE '" + cmbNDate.Text.ToString().Trim() + "%'";
                filter = "CUSTOMER LIKE '" + this.txtBrand.Text.Trim() + "%' AND CHAR5 LIKE '" + this.txtBuyerName.Text.Trim() 
                    + "%' AND VKPART LIKE '" + this.txtBarcode.Text.Trim() + "%' AND VKPARTDES LIKE '"+txtART.Text.ToString().Trim()+"%'";
                cvMaster.CustomFilter = filter;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }

        }
        private void btnFilterClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                cvMaster.CustomFilter = null;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void UploadCommand(object sender, RoutedEventArgs e)
        {
            string sql = "SELECT * FROM ICC_MAXORD";
            TBResult = TMV.SQLiteDatabase.GetDataTable(sql);
            string lastord = "0";
            foreach (DataRow row in TBResult.Rows)
            {
                lastord = Helper.Right("000000" + row["USD2RMB"].ToString(), 6);
            }
            DataSet Set = new DataSet();
            TMV.sql = "SELECT * FROM ICC_EXORD WHERE VKPAUFKRS='"+app.TERMNO.PadRight(2).Substring(0,2)+"' AND VKPAUFLFD>='"+lastord+"'";
            TBResult = TMV.SQLiteDatabase.GetDataTable(TMV.sql); 
            Set.Merge(TBResult);
            try
            {
                foreach(DataRow row in TBResult.Rows)
                {
                    TMV.InsertIITXEXORD(row);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void DownloadCommand(object sender, RoutedEventArgs e)
        { 
            dwData = TBMain.NewRow();
            dwData["VKPAUFKRS"] = KRS.PadRight(2).Substring(0, 2);
            SResult = TMV.QEXORD02(dwData);
            MergeData(SResult, false);
        }

        private void Export2PrintArticleCommand(object sender, RoutedEventArgs e)
        {
            EXAUF = "";
            if (dwMaster == null)
                return;
            filter = "VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            sort = "VKPAUFPOS DESC";
            rows = TBMain.Select(filter, sort);
            foreach (DataRow row in rows)
            {
                EXAUF = "".PadRight(5)+ row["VKPART"].ToString();
            }
        }
    }
}